# 精英杂志PWA功能Git提交脚本 - PowerShell版本

Write-Host "🚀 开始PWA功能Git提交..." -ForegroundColor Green

# 设置编码
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8

# 进入项目目录
Set-Location "C:\Users\Administrator\Documents\trae_projects\lt00001\magazine"

Write-Host "📁 当前目录: $(Get-Location)" -ForegroundColor Yellow

# 检查Git是否在PATH中
try {
    $gitVersion = git --version
    Write-Host "✅ Git已安装: $gitVersion" -ForegroundColor Green
} catch {
    Write-Host "❌ Git未找到，正在查找Git安装位置..." -ForegroundColor Red
    
    # 常见Git安装路径
    $gitPaths = @(
        "C:\Program Files\Git\bin\git.exe",
        "C:\Program Files (x86)\Git\bin\git.exe",
        "C:\Users\$env:USERNAME\AppData\Local\Programs\Git\bin\git.exe"
    )
    
    $gitFound = $false
    foreach ($path in $gitPaths) {
        if (Test-Path $path) {
            Write-Host "✅ 找到Git: $path" -ForegroundColor Green
            $env:PATH = "$(Split-Path $path);$env:PATH"
            $gitFound = $true
            break
        }
    }
    
    if (-not $gitFound) {
        Write-Host "❌ 未找到Git安装，请手动安装Git" -ForegroundColor Red
        Write-Host "📥 下载地址: https://git-scm.com/download/win" -ForegroundColor Yellow
        exit 1
    }
}

# 检查Git状态
Write-Host "📊 检查Git状态..." -ForegroundColor Yellow
git status

# 配置用户信息（如果尚未配置）
try {
    $userName = git config user.name
    if ([string]::IsNullOrEmpty($userName)) {
        Write-Host "🔧 配置Git用户信息..." -ForegroundColor Yellow
        $name = Read-Host "请输入你的Git用户名"
        $email = Read-Host "请输入你的Git邮箱"
        git config --global user.name $name
        git config --global user.email $email
    } else {
        Write-Host "✅ Git用户已配置: $userName" -ForegroundColor Green
    }
} catch {
    Write-Host "🔧 配置Git用户信息..." -ForegroundColor Yellow
    $name = Read-Host "请输入你的Git用户名"
    $email = Read-Host "请输入你的Git邮箱"
    git config --global user.name $name
    git config --global user.email $email
}

# 添加PWA相关文件
Write-Host "📝 添加PWA文件..." -ForegroundColor Yellow

# PWA核心文件
git add manifest.json, service-worker.js, offline.html, static/offline-test.html

# CSS文件
git add static/css/mobile-optimization.css, static/css/pwa-styles.css, static/css/pwa-install.css

# JavaScript文件  
git add static/js/pwa-installer.js, static/js/pwa-install.js

# 主题模板
git add themes/magazine-theme/layouts/_default/baseof.html, themes/magazine-theme/layouts/index.html

# 文档和工具
git add PWA_IMPLEMENTATION_SUMMARY.md, validate_pwa.py, simple_server.py, git-add-pwa-files.bat, commit-pwa-features.bat

# 其他文件
git add static/images/icons/, .github/workflows/sync-to-gitee.yml

Write-Host "✅ 文件添加完成" -ForegroundColor Green

# 提交更改
Write-Host "💾 提交更改..." -ForegroundColor Yellow
$commitMessage = @"
feat: 添加完整的PWA功能实现

- 添加Web App Manifest配置
- 实现Service Worker缓存策略
- 添加PWA安装提示功能
- 优化移动端适配和响应式设计
- 添加离线功能和测试工具
- 更新主题模板支持PWA
"@

git commit -m $commitMessage

# 推送到远程仓库
Write-Host "🔄 推送到远程仓库..." -ForegroundColor Yellow

# 尝试推送到main分支
try {
    git push -u origin main
    Write-Host "✅ 推送到main分支成功！" -ForegroundColor Green
} catch {
    Write-Host "⚠️ 推送到main分支失败，尝试master分支..." -ForegroundColor Yellow
    try {
        git push -u origin master
        Write-Host "✅ 推送到master分支成功！" -ForegroundColor Green
    } catch {
        Write-Host "❌ 推送失败！" -ForegroundColor Red
        Write-Host "🔧 可能的原因和解决方案：" -ForegroundColor Yellow
        Write-Host "   1. 远程仓库地址未配置" -ForegroundColor White
        Write-Host "      解决：git remote add origin https://gitee.com/你的用户名/magazine.git" -ForegroundColor White
        Write-Host "   2. 需要身份验证" -ForegroundColor White
        Write-Host "      解决：使用HTTPS方式推送，输入用户名和密码" -ForegroundColor White
        Write-Host "   3. 分支不存在" -ForegroundColor White
        Write-Host "      解决：先在远程仓库创建分支" -ForegroundColor White
    }
}

Write-Host "🎉 操作完成！" -ForegroundColor Green
Write-Host "📋 如果推送成功，GitHub Actions将自动同步到Gitee" -ForegroundColor Yellow

# 暂停查看结果
Write-Host "按任意键退出..." -ForegroundColor Gray
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")